package com.xiaoyu.awakening.ai.control;

import com.alibaba.cloud.ai.autoconfigure.dashscope.DashScopeChatAutoConfiguration;
import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.alibaba.cloud.ai.graph.agent.ReactAgent;
import com.alibaba.cloud.ai.graph.checkpoint.savers.MemorySaver;
import com.alibaba.cloud.ai.graph.exception.GraphRunnerException;
import com.xiaoyu.awakening.ai.control.tool.WeatherTool;
import jakarta.annotation.PostConstruct;
import lombok.extern.log4j.Log4j2;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.ai.tool.function.FunctionToolCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
 * 基础组件服务
 * AwakeningBaseApplication
 *
 * @author wangxiaoyu
 * @date 2024/6/6 15:18
 * @since v1.0
 */
@SpringBootApplication
@ComponentScan("com.xiaoyu.awakening.*")
@Log4j2
public class AwaAiControlApplication {
    public static void main(String[] args) {
        SpringApplication.run(AwaAiControlApplication.class, args);
    }

    @PostConstruct
    public void testAgent() {
        DashScopeApi dashScopeApi = DashScopeApi.builder().apiKey("sk-0ac1272af91340ab91dc74d62de0b23f").build();

        ChatModel chatModel = DashScopeChatModel.builder().dashScopeApi(dashScopeApi).build();

        ToolCallback weatherTool = FunctionToolCallback.builder("get_weather", new WeatherTool()).description("Get weather for a given city").inputType(String.class).build();

        // 创建 agent
        ReactAgent agent = ReactAgent.builder().name("weather_agent").model(chatModel).tools(weatherTool).systemPrompt("You are a helpful assistant").saver(new MemorySaver()).build();

        // 运行 agent
        AssistantMessage response = null;
        try {
            response = agent.call("what is the weather in San Francisco");
        } catch (GraphRunnerException e) {
            throw new RuntimeException(e);
        }
        log.info(response.getText());
    }
}
